package ui;

import com.sun.awt.AWTUtilities;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.*;

/**
 * <p> 显示鼠标位置面板 </p>
 *
 * @author gmr
 * @date 2020/5/9
 */
public class MousePanel extends JFrame implements ActionListener {
    JButton btnClose;
    static JLabel lblTip;
    private static MousePanel mousePanel = null;
    private MousePanel(){
        setExtendedState(this.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        setLayout(null);

        btnClose = new JButton("X");
        btnClose.setBackground(Color.RED);
        btnClose.setMargin(new Insets(0,0,0,0));
        btnClose.setForeground(Color.white);
        btnClose.setBounds(Toolkit.getDefaultToolkit().getScreenSize().width-45,0,40,25);
        //除去焦点的框
        btnClose.setFocusPainted(false);
        btnClose.addActionListener(this);

        lblTip = new JLabel();
        lblTip.setBounds(0,0,110,30);
        lblTip.setForeground(Color.white);
        lblTip.setBackground(new Color(0,0,0,0.6f));
        lblTip.setOpaque(true);
        lblTip.setBorder(new EmptyBorder(0,10,0,10));

        add(btnClose);
        add(lblTip);

        addMouseMotionListener(new MyMouseListener());

        setUndecorated(true);
        //透明度
//        AWTUtilities.setWindowOpacity(this, 0.1f);
        setBackground(new Color(0,0,0,0.01f));
//        AWTUtilities.setWindowOpaque(this,false);
        getRootPane().setBorder(BorderFactory.createLineBorder(Color.CYAN,2));
        setResizable(false);
        setAlwaysOnTop(true);
        setVisible(true);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    public static MousePanel getInstance(){
        if(mousePanel == null){
            mousePanel = new MousePanel();
        }
        return mousePanel;
    }

    public static void main(String[] args) {
        new MousePanel();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == btnClose){
//            MousePanel.getInstance().dispose();
            MousePanel.getInstance().dispatchEvent(new WindowEvent(MousePanel.getInstance(),WindowEvent.WINDOW_CLOSING) );
            mousePanel = null;
        }
    }

    static class MyMouseListener extends MouseAdapter{
        @Override
        public void mouseMoved(MouseEvent e) {
            int x = e.getX();
            int y = e.getY();

            int lx = x + 10;
            int ly = y + 10;

            int width = Toolkit.getDefaultToolkit().getScreenSize().width;
            int height = Toolkit.getDefaultToolkit().getScreenSize().height;
            if(lx > width - lblTip.getWidth()){
                lx = width - lblTip.getWidth() - 10;
            }
            if(ly > height - lblTip.getHeight()){
                ly = height - lblTip.getHeight();
            }
//            System.out.println(lx + "," + ly);
            lblTip.setLocation(lx,ly);
            lblTip.setText("X: " + x + ", " + "Y: " + y);
        }

    }

}
